package com.cnswhy.cloud.server.core.utils.BaiduAip;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.util.Base64Util;
import com.cnswhy.cloud.server.core.entity.FaceResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

import java.net.URLEncoder;

/**
 * ClaseName：FaceMatch
 * Description：
 * Author：JensenWei
 * QQ: 2188307188
 * Createtime：2018/3/10 10:54
 * Modified By：
 * Fixtime：2018/3/10 10:54
 * FixDescription：
 */
@Slf4j
public class FaceMatch {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */

    public static String match(String filePath, String filePath2) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v2/match";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            if (imgData == null) {
                return null;
            }
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            byte[] imgData2 = FileUtil.readFileByBytes(filePath2);
            if (imgData2 == null) {
                return null;
            }
            String imgStr2 = Base64Util.encode(imgData2);
            String imgParam2 = URLEncoder.encode(imgStr2, "UTF-8");

            String param = "images=" + imgParam + "," + imgParam2 + "&image_liveness=,faceliveness";

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。

            String accessToken = AuthService.getAuth();
            String result = HttpUtil.post(url, accessToken, param);
            log.info(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String result = FaceMatch.match("http://121.201.20.215/group1/M00/04/A9/wKhkclq9476AP8e2AAJUjEiR6U8505.jpg", "http://121.201.20.215/group1/M00/04/A9/wKhkclq9422ANengAAEm7qFCae4634.jpg");
        FaceResult faceResult = JSON.parseObject(result, FaceResult.class);
        if (faceResult.getResult_num() < 1) {
            System.out.println("对比图片中有图片无人像");
//            return ResultGenerator.genFailResult("人像识别不出，请上传重试");
        } else {
            if (faceResult.getResult().get(0).getScore() < 80) {
                System.out.println("对比结果匹配度低于80");
//                return ResultGenerator.genFailResult("匹配度不足，请上传重试");
            }
            if (faceResult.getExt_info().getFaceliveness() != null) {
                String Faceliveness = faceResult.getExt_info().getFaceliveness();
                String s[] = Faceliveness.split(",");
                Double score = Double.parseDouble(s[1]);
                if (score < 0.393241) {
                    System.out.println("人脸图片不是活体图片");
                } else {
                    System.out.println("对比通过");
                }
            }

        }
    }
}
